﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrainTechLLC.EmlenMud.Communication;
using BrainTechLLC;
using BrainTechLLC.ThreadSafeObjects;
using System.Net;
using System.Threading;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using BrainTechLLC.ImageOperatorsNonAForgeDependent;
using BrainTechLLC.WebCamLib3;

namespace WpfImageBroadcaster
{
    public class Broadcaster
    {
        protected CameraHandler _cameraHandler;
        protected CameraControl _cameraControl;
        protected Communications _server;

        public void StartBroadcasting()
        {
            _server.Startup();
        }

        public Broadcaster(Communications server, CameraControl cameraControl)
        {
            _server = server;
            _cameraControl = cameraControl;
            _cameraControl.CameraHandlerCreated += new EventHandler<ObjectEventArgs<CameraHandler>>(cameraControl_CameraHandlerCreated);
        }

        void cameraControl_CameraHandlerCreated(object sender, ObjectEventArgs<CameraHandler> e)
        {
            _cameraControl.CameraHandlerCreated -= cameraControl_CameraHandlerCreated;
            e.obj.NewFrameReceived += new EventHandler<ObjectEventArgs<FrameInfo>>(_cameraHandler_NewFrameReceived);
        }

        public Broadcaster(Communications server, CameraHandler cameraHandler)
        {
            _server = server;
            _cameraHandler = cameraHandler;
            _cameraHandler.NewFrameReceived += new EventHandler<ObjectEventArgs<FrameInfo>>(_cameraHandler_NewFrameReceived);            
        }

        void _cameraHandler_NewFrameReceived(object sender, ObjectEventArgs<FrameInfo> e)
        {
            Bitmap bm = e.obj.CreateBitmap();
            _server.OutgoingBitmapQueue.Enqueue(new Bitmap(e.obj.Bitmap));
        }
    }
}
